package com.imay.live.activity.family;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.manager.SensitiveWordFilterManager;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ToastUtil;
import com.imay.live.utils.ViewUtil;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by Administrator on 2017/9/3 0003.
 */

public class FamilyJoinActivity extends BaseRequestActivity {

    private EditText et_family_join_explain;
    private TextView tv_family_join_count;

    private int familyid;

    private static final String KEY_FAMILY_ID = "key_family_id";

    private String requestJoinFamilyCommand;
    private final int count = 50;

    /**
     * @param context
     */
    public static void startActivity(Context context, int familyid) {
        Intent intent = new Intent(context, FamilyJoinActivity.class);
        intent.putExtra(KEY_FAMILY_ID, familyid);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family_join;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        familyid = getIntent().getIntExtra(KEY_FAMILY_ID, -1);
        setTitle(R.string.str_family_apply);
        getTitleBar().setLeftImageVisibility(false);
        getTitleBar().setLeftTextVisibility(true);
        getTitleBar().addLeftTextView(getString(R.string.str_family_join_cancel));
        getTitleBar().setRightImageVisibility(false);
        getTitleBar().setRightTextVisibility(true);
        getTitleBar().addRightTextView(getString(R.string.str_family_join_ok));
        getTitleBar().setRightTextOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestJoinFamily();
            }
        });
        getTitleBar().setLeftTextOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ViewUtil.setForgroundRadius($(R.id.content));
        et_family_join_explain = ViewUtil.findView(FamilyJoinActivity.this, R.id.et_family_join_explain);
        tv_family_join_count = ViewUtil.findView(FamilyJoinActivity.this, R.id.tv_family_join_count);

        et_family_join_explain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_family_join_count.setText(getString(R.string.str_family_create_note3, count - s.length()));
            }
        });
    }

    private void requestJoinFamily() {
        if (familyid == -1) {
            return;
        }
        if (SensitiveWordFilterManager.getInstance().hasSensitiveWord(et_family_join_explain)) {
            ToastUtil.show(this, R.string.sensitive_comment);
            return;
        }
        String explain;
        if (TextUtils.isEmpty(et_family_join_explain.getText())) {
            explain = et_family_join_explain.getHint().toString();
        } else {
            explain = et_family_join_explain.getText().toString();
        }
        requestJoinFamilyCommand = ProtoRequestHelper.task(ProtoHttpFactory.joinFamily(familyid, explain)).setTag(hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command.equals(requestJoinFamilyCommand)) {
            Result<String> result = JSONObject.parseObject(data, new TypeReference<Result<String>>() {
            }.getType());
            if (result.result == CodeSuccess) {
                ToastUtil.show(FamilyJoinActivity.this, "申请成功,等待审核");
                finish();
            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        ErrorResult.toastError(this, code);
    }
}
